#version 300 es

#ifdef GL_ES
precision mediump float;
#endif

#define PI 3.1415926
#define TWO_PI 6.28318530718

uniform vec2 u_resolution;
uniform float u_time;
float minStep = 0.01;

float ploygon(vec2 st, int N, vec2 pos, float size) {

  vec2 p = st - pos;
  float a = mod(atan(p.y, p.x) + TWO_PI, TWO_PI);
  float r = TWO_PI / float(N);
  // color = vec3(a / TWO_PI);
  float d = 0.0;

  // d = (floor(a / r + 0.5) * r) / TWO_PI;

  d = cos(a - floor(a / r + 0.5) * r) * length(p) / size;
  d = 1.0 - smoothstep(0.2, 0.21, d);

  return d;
}

out vec4 outColor;

void main() {

  float solution = min(u_resolution.x, u_resolution.y);
  minStep = 1.0 / solution;
  vec2 st = (gl_FragCoord.xy - u_resolution / 2.0) * minStep;
  vec2 s = vec2(1.0, 1.0) - step(0.5, abs(st));
  if(s.x * s.y < 0.00001) {
    discard;
  }

  vec3 color = vec3(0.0);

  float pct = 0.0;
  pct = ploygon(st, 3, vec2(-0.25, 0.25), 0.5);
  color = mix(color, vec3(0.0, 1.0, 0.0), pct);

  pct = ploygon(st, 4, vec2(0.25, 0.25), 0.5);
  color = mix(color, vec3(1.0, 0.0, 0.0), pct);

  pct = ploygon(st, 5, vec2(-0.25, -0.25), 0.5);
  color = mix(color, vec3(1.0, 0.0, 1.0), pct);

  pct = ploygon(st, 6, vec2(0.25, -0.25), 0.5);
  color = mix(color, vec3(1.0, 1.0, 0.0), pct);

  outColor = vec4(color, 1.0);
}
